# !/usr/bin/env python
# -*- coding:utf-8 -*-
import json
import re

from pyquery import PyQuery

from base.gsxt_base_worker import GsxtBaseWorker
from common.global_field import Model

'''
1. 搜索没有结果判断功能添加
'''


class GsxtGuiZhouWorker(GsxtBaseWorker):
    def __init__(self, **kwargs):
        GsxtBaseWorker.__init__(self, **kwargs)

    def get_search_result_html(self, keyword, session):
        param_list = []
        try:
            url = 'http://{host}'.format(host=self.host)
            content = self.get_captcha_geetest(url, '#q', '#search_s', keyword,
                                               '#list > div:nth-child(1) > dt > a',
                                               success="#list",
                                               proxy_type=2)
            if content is None:
                return param_list, self.SEARCH_ERROR

            # 增加无搜索结果过滤
            if content.find('您输入的查询条件未查询到相关记录') != -1:
                return param_list, self.SEARCH_NOTHING_FIND

            pattern = '"javascript:showDetail\(&quot;(.*?)&quot;,&quot;(.*?)&quot;,&quot;(.*?)&quot;,&quot;(.*?)&quot;,&quot;(.*?)&quot;\);"'
            regex = re.compile(pattern)
            search_list = regex.findall(content)
            if len(search_list) > 0:
                for item in search_list:
                    param_list.append({
                        'nbxh': item[0],
                        'qymc': item[1],
                        'zch': item[2],
                        'ztlx': item[3],
                        'qylx': item[4],
                    })
        except Exception as e:
            self.log.exception(e)
            return param_list, self.SEARCH_ERROR

        return param_list, self.SEARCH_SUCCESS if len(param_list) > 0 else self.SEARCH_ERROR

    def __get_company_name(self, text):
        try:
            result_json = json.loads(text)
            company_name = result_json['data'][0]['qymc']
            return company_name
        except Exception as e:
            self.log.exception(e)

        return None

    def get_year_info_list(self, text):
        jq = PyQuery(text, parser='html')
        item_list = jq.find("#yearreportTable").find('tr').items()
        for item in item_list:
            try:
                if item.text().find('年度报告') == -1:
                    continue
                year_info = item.find('td').eq(1)
                if year_info is None or year_info == '':
                    continue

                year_list = re.findall('(\d+)', year_info.text())
                year = str(year_list[0]) if len(year_list) > 0 else None
                if year is None:
                    continue

                href = item.find('a').attr('href')
                if href is None or href == '':
                    continue

                yield year, 'http://{host}{href}'.format(host=self.host, href=href)
            except Exception as e:
                self.log.exception(e)

    # 基本信息
    def get_base_info(self, session, url, post_data):
        # 获取基础信息
        base_info = self.task_request(session.post, url=url, data=post_data)
        if base_info is None:
            self.log.error('获取基础信息页面失败...')
            return None
        return base_info.text

    # 主要人员
    def get_key_person_info(self, session, url, post_data, data):
        r = self.task_request(session.post, url=url, data=post_data)
        if r is None:
            return
        self.append_model(data, Model.key_person_info, url + '#' + Model.key_person_info, r.text, post_data=post_data)

    # 变更信息
    def get_change_info(self, session, url, post_data, data):
        r = self.task_request(session.post, url=url, data=post_data)
        if r is None:
            return
        self.append_model(data, Model.change_info, url + '#' + Model.change_info, r.text, post_data=post_data)

    # def get_annual_info(self, session, text, data, totol_data):
    #     # 暂时不处理 跟解析沟通一下
    #     for year, url in self.get_year_info_list(text):
    #         r = self.task_request(session.get, url)
    #         if r is None:
    #             continue
    #         self.append_model(totol_data, Model.annual_info, url, r.text, year=year, classify=Model.type_detail)

    def get_shareholder_info(self, session, url, post_data, data):
        try:
            r = self.task_request(session.post, url=url, data=post_data)
            if r is None:
                return
            self.append_model(data, Model.shareholder_info, url + '#' + Model.shareholder_info, r.text,
                              post_data=post_data)
        except Exception as e:
            self.log.exception(e)

    def get_branch_info(self, session, url, post_data, data):
        r = self.task_request(session.post, url=url, data=post_data)
        if r is None:
            return
        self.append_model(data, Model.branch_info, url + '#' + Model.branch_info, r.text, post_data=post_data)

    def get_contributive_info(self, session, url, post_data, data):
        try:
            r = self.task_request(session.post, url=url, data=post_data)
            if r is None:
                return
            self.append_model(data, Model.contributive_info, url + '#' + Model.contributive_info, r.text,
                              post_data=post_data)
            return
        except Exception as e:
            self.log.exception(e)

    def get_detail_html_list(self, seed, session, param_list):
        # 保存企业名称
        data_list = []
        rank = 0
        for item in param_list:
            try:
                param_dict = item
                if 'nbxh' not in param_dict:
                    self.log.error('参数存储异常: item = {item}'.format(item=item))
                    continue
                url = 'http://gz.gsxt.gov.cn/2016/gfgs/query!searchData.shtml'
                # 基本信息
                base_info_data = {
                    'c': '0',
                    'nbxh': param_dict['nbxh'],
                    't': '5',
                }
                base_text = self.get_base_info(session, url, base_info_data)
                if base_text is None:
                    continue
                company_name = self.__get_company_name(base_text)
                if company_name is None or company_name == '':
                    self.log.error('公司名称解析失败..item = {item} {text}'.format(
                        text=base_text, item=item))
                    continue
                # 建立数据模型
                rank += 1
                data = self.get_model(company_name, rank, seed, self.province)

                # 存储数据
                self.append_model(data, Model.base_info, url + '#' + Model.base_info, base_text, post_data=param_dict)

                member_data = {
                    'c': '0',
                    'nbxh': param_dict['nbxh'],
                    't': '8'
                }

                branch_data = {
                    'c': '0',
                    'nbxh': param_dict['nbxh'],
                    't': '9'
                }

                contributive_data = {
                    'c': '2',
                    'nbxh': param_dict['nbxh'],
                    't': '3'
                }
                change_data = {
                    'c': '0',
                    'nbxh': param_dict['nbxh'],
                    't': '3'
                }
                investor_data = {
                    'c': '0',
                    'nbxh': param_dict['nbxh'],
                    't': '40'
                }

                # 主要人员信息
                self.get_key_person_info(session, url, member_data, data)

                # 分支机构
                self.get_branch_info(session, url, branch_data, data)

                # 变更信息
                self.get_change_info(session, url, change_data, data)

                # 获得年报信息
                # 6.5W家年报暂时满足要求 可暂缓
                # self.get_annual_info(session, base_text, data)

                # 股东信息
                self.get_shareholder_info(session, url, investor_data, data)

                # 出资信息
                self.get_contributive_info(session, url, contributive_data, data)

                data_list.append(data)
            except Exception as e:
                self.log.exception(e)
        self.target_db.insert_batch_data(self.target_table, data_list)
        return len(data_list)
